// Learn TypeScript:
//  - https://docs.cocos.com/creator/manual/en/scripting/typescript.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/manual/en/scripting/life-cycle-callbacks.html

import Shader from "../Shader";

const { ccclass, property, executeInEditMode } = cc._decorator;

@ccclass
export default class WaterShader extends Shader {
    @property(cc.Texture2D)
    _noiseMap: cc.Texture2D = null;

    @property(cc.Texture2D)
    get noiseMap() {
        return this._noiseMap;
    }

    set noiseMap(value) {
        if (value !== this._noiseMap) {
            this._noiseMap = value;
            this.updateNoiseMap();
        }
    }

    @property
    _speed: cc.Vec2 = cc.Vec2.ZERO;

    @property({
        range: [-1, 1],
        tooltip: 'x 方向流动速度'
    })
    get xSpeed() {
        return this._speed.x;
    }

    set xSpeed(value) {
        if (value !== this._speed.x) {
            this._speed.x = value;
            this.updateSpeed();
        }
    }

    @property({
        range: [-1, 1],
        tooltip: 'y 方向流动速度'
    })
    get ySpeed() {
        return this._speed.y;
    }

    set ySpeed(value) {
        if (value !== this._speed.y) {
            this._speed.y = value;
            this.updateSpeed();
        }
    }

    @property
    _range: cc.Vec2 = cc.Vec2.ZERO;

    @property({
        range: [0, 1],
        tooltip: 'x 方向扰动幅度'
    })
    get xRange() {
        return this._range.x;
    }

    set xRange(value) {
        if (value !== this._range.x) {
            this._range.x = value;
            this.updateRange();
        }
    }

    @property({
        range: [0, 1],
        tooltip: 'y 方向扰动幅度'
    })
    get yRange() {
        return this._range.y;
    }

    set yRange(value) {
        if (value !== this._range.y) {
            this._range.y = value;
            this.updateRange();
        }
    }

    protected start(): void {
        this.updateNoiseMap();
        this.updateSpeed();
        this.updateRange();
    }

    private updateNoiseMap() {
        this.setProperty("noiseMap", this.noiseMap);
    }

    private updateSpeed() {
        this.setProperty("u_speed", this._speed);
    }

    private updateRange() {
        this.setProperty("u_range", this._range);
    }
}
